#-*- coding: utf-8 -*-
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from gironimo.blog.models import Post, PostRevision


class PostRevisionInline(admin.TabularInline):
    model = PostRevision
    extra = 1


class PostAdmin(admin.ModelAdmin):
    date_hierarchy = 'created'
    list_display = ('user', 'slug', 'created')
    inlines = (PostRevisionInline,)


class PostRevisionAdmin(admin.ModelAdmin):
    date_hierarchy = 'created'
    list_filter = ('status',)
    list_display = ('get_revision_title', 'created', 'status',)
    list_per_page = 20
    ordering = ['-created', '-status',]
    search_fields = ['title',]
    actions = [
        'publish_revision',
    ]
    fieldsets = [
        (_('Eintrag'), {'fields': ['status', 'title', 'content', 'category', 'pic', 'tags',]}),
        (_('HTML'),    {'fields': ['html_title', 'html_description', 'html_keywords',], 'classes': ['collapse']}),
    ]
    
    def get_revision_title(self, obj):
        return '%s - %d.%d.%d' % (obj.title, obj.created.day, obj.created.month, obj.created.year)
    get_revision_title.short_description = _('Revisiontitel')
    
    def publish_revision(self, request, queryset):
        for obj in queryset:
            obj.publish()
        self.message_user(request, "%d Revisionen veroeffentlicht" % queryset.count())
    publish_revision.short_description = _('Revision veroeffentlichen')


admin.site.register(Post, PostAdmin)
admin.site.register(PostRevision, PostRevisionAdmin)
